import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    name: 'Root',
    meta: {
      hidden: true,
      icon: 'ic:outline-home'
    },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: {
          title: t('router.home'),
          icon: 'ic:outline-home',
          affix: true
        }
      }
    ]
  },
  {
    path: '/syscontent',
    component: Layout,
    redirect: '/syscontent/index',
    name: 'SysContent',
    meta: {
      title: t('router.syscontent'),
      icon: 'fluent:content-view-20-regular'
    },
    children: [
      {
        path: 'testVideo',
        name: 'testVideo',
        component: () => import('@/views/SysContent/testVideo.vue'),
        meta: {
          title: '视频播放'
        }
      },
      {
        path: 'index',
        name: 'sysContentIndex',
        component: () => import('@/views/SysContent/Index.vue'),
        meta: {
          title: t('router.moduleIndex'),
          icon: 'fluent-mdl2:entry-view'
        }
      },
      {
        path: 'dataindex',
        name: 'sysContentDataIndex',
        component: () => import('@/views/SysContent/DataIndex.vue'),
        meta: {
          title: t('router.manageDataIndex'),
          icon: 'octicon:database-24'
        }
      },
      {
        path: 'flexdatalib',
        name: 'sysContentFlexDataLib',
        component: () => import('@/views/SysContent/FlexDataLib.vue'),
        meta: {
          title: t('router.manageFlexDataLib'),
          icon: 'fluent-mdl2:data-management-settings'
        }
      },
      {
        path: 'varietyformula',
        name: 'sysContentVarietyFormula',
        component: () => import('@/views/SysContent/VarietyFormula.vue'),
        meta: {
          title: t('router.manageVarietyFormula'),
          icon: 'fluent:math-formula-20-filled'
        }
      },
      {
        path: 'researchFile',
        name: 'sysContentResearchFile',
        component: () => import('@/views/SysContent/ResearchFile.vue'),
        meta: {
          title: t('router.manageResearchFile'),
          icon: 'fluent:document-pdf-24-regular'
        }
      }
    ]
  },
  {
    path: '/exdoctor',
    component: Layout,
    redirect: '/exdoctor/index',
    name: 'Exdoctor',
    meta: {
      title: t('router.exdoctor'),
      icon: 'healthicons:doctor-male-outline'
    },
    children: [
      {
        path: 'index',
        name: 'exdoctorIndex',
        component: () => import('@/views/Exdoctor/Index.vue'),
        meta: {
          title: t('router.moduleIndex'),
          icon: 'fluent-mdl2:entry-view'
        }
      },
      {
        path: 'bindcust',
        name: 'BindCust',
        component: () => import('@/views/Exdoctor/BindCustNum.vue'),
        meta: {
          title: t('router.BindCust'),
          icon: 'zondicons:link'
        }
      }
    ]
  },
  {
    path: '/sysmanage',
    component: Layout,
    redirect: '/sysmanage/index',
    name: 'SysManage',
    meta: {
      title: t('router.sysmanage'),
      icon: 'arcticons:activity-manager'
    },
    children: [
      {
        path: 'index',
        name: 'sysManageIndex',
        component: () => import('@/views/SysManage/Index.vue'),
        meta: {
          title: t('router.moduleIndex'),
          icon: 'fluent-mdl2:entry-view'
        }
      },
      {
        path: 'user',
        name: 'sysManageUser',
        component: () => import('@/views/SysManage/User.vue'),
        meta: {
          title: t('router.manageUser'),
          icon: 'ic:outline-manage-accounts'
        }
      },
      {
        path: 'variety',
        name: 'sysManageVariety',
        component: () => import('@/views/SysManage/Variety.vue'),
        meta: {
          title: t('router.manageVariety'),
          icon: 'eos-icons:virtual-host-manager'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root', 'Home']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
  console.log('退出重置路由后', router.getRoutes())
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
